//! \file backtacer_debug.c

#include <assert.h>

#include "debug.h"
#include "backtracer_linux_arm.h"
#include "linux_ptrace_utils.h"

//------------------------------------------------------------------------------

void _Map_Dump(func_map *pmap) {
    assert(pmap && "Invalid func_map pointer");

    printf("\n-------------------------\n");
    printf("Current memory map:\n");

    assert((pmap->map_ || pmap->size_ == 0) && "invalid pmap");

    unsigned int i = 0;
    for(i = 0; i < pmap->size_; ++i) {
        assert(pmap->map_[i].name_idx_ < pmap->size_);
        printf("%s: [%x]\n", pmap->names_[pmap->map_[i].name_idx_],
              (unsigned int) pmap->map_[i].addr_);
    }
    printf("\n-------------------------\n");
}

//------------------------------------------------------------------------------

TBGError PtraceDump(ptrace_session *pps) {
    CHECK_DBG_ERR(pps, NULL_PTR);

    printf("\n-------------------------\n");
    printf("Current ptrace_session:\n");
    printf("is %s attached", pps->attached ? "" : "NOT");
    printf("pid = [%d]\n", pps->pid);
    printf("\n-------------------------\n");
    return 0;
} 

//------------------------------------------------------------------------------

void PrintStack() {
    printf("\nCurrent stack status is:\n");
    addr_t addr = 0, res = 0;
    int max_offset = 2048;
  
    addr_t offset = 0; 
    for(offset = 0; offset < max_offset; offset += 4) {
        asm volatile("ldr %[res], [sp, %[offset]]\n\t"
                     "add %[addr], sp, %[offset]\n\t"
                     : [res] "=r" (res), [addr] "=r" (addr)
                     : [offset] "r" (offset));
        printf("%x: [%x]\n", (unsigned int) addr, (unsigned int) res);  
    }
}  

